/*
 * @Author: zxh
 * @Email: 1271027008@qq.com
 * @Date: 2020-02-24 08:53:29
 * @Descripttion: 库房安全库存管理
 */

import { Component, OnInit } from '@angular/core';
import { SafemanageListService } from './safemanage-list.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { HCommonService } from 'src/app/h-common/h-common.service';

@Component({
  selector: 'app-safemanage-list',
  templateUrl: './safemanage-list.component.html',
  styleUrls: ['./safemanage-list.component.scss']
})
export class SafemanageListComponent implements OnInit {

  constructor(
    private SafemanageListService: SafemanageListService,
    private NzMessageService: NzMessageService,
    private NzModalService: NzModalService,
    private HCommonService:HCommonService
  ) { }

  ngOnInit() {
    /* 获取权限 */
    this.power = this.HCommonService.getRoles("stock");
    console.log( this.power );
    this.getKFList();
    this.getKQAllList();
    this.getCLList();
  }

  pageSize: number = 10
  KFList = []
  KQList = []
  CLList = []

  // 获取库房列表
  getKFList() {
    this.SafemanageListService.getKFList().subscribe((res) => {
      this.KFList = res
    })
  }
  getKQList() {
    let kfId = this.kfID
    for (let i = 0; i < this.KFList.length; i++) {
      if (this.KFList[i].id == kfId) {
        this.KQList = this.KFList[i].warehouseAreas
      }
    }
  }
  // 获取存料列表
  getCLList() {
    let _dataJson = {
      dictName: 'material_category'
    }
    this.SafemanageListService.getCLList(_dataJson).subscribe(res => {
      this.CLList = res
    })
  }

  // search start
  kfID: string = '';//库房库区
  kqID: string = '';
  clType: string = '';//存料类型
  selectedKF(data) {
    this.kfID = data
    this.kqID = ''
    this.getKQList()
    this.getKQAllList('search')
    this.bottomListOfData = []
  }
  selectedKQ(data) {
    this.kqID = data
    this.getKQAllList('search')
    this.bottomListOfData = []
  }
  selectedCl(data) {
    this.clType = data
    this.getKQAllList('search')
    this.bottomListOfData = []
  }
  // search end

  // table 1 start
  topListOfData = [];
  totalSizeTop: number;
  topIndex: number = 1;
  selectedKQId: number;
  selectedKFId: number;
  getKQAllList(type?) {
    if (type == 'search') {
      this.topIndex = 1
      this.selectedKQId = 0
      this.selectedKFId = 0
    }
    let _data = {
      warehouseId: this.kfID,
      warehouseAreaId: this.kqID,
      materialCategory: this.clType,
      page: this.topIndex - 1,
      size: this.pageSize
    }
    this.SafemanageListService.getKQList(_data).subscribe(res => {
      this.topListOfData = res.content
      this.totalSizeTop = res.totalElements
    })
  }
  clickPageTop(index) {
    this.topIndex = index
    this.getKQAllList()
  }
  selectDetail(KQId, KFId) {
    this.selectedKQId = KQId
    this.selectedKFId = KFId
    this.mapOfCheckedId = {}
    this.numberOfChecked = 0
    this.idList = []
    this.getMaterialList()
  }
  // table 1 end

  power:any

  materialCode: string = '';//物料编码
  materialName: string = '';//物料名称
  // table 2 start
  page: number = 1;
  totalSize: number = 0;
  getMaterialList(type?) {
    if (type === 'search') {
      this.page = 1
    }
    return new Promise((resolve, reject) => {
      try {
        let _dataJson = {
          warehouseId: this.selectedKFId,
          warehouseAreaId: this.selectedKQId,
          materialCode: this.materialCode,
          materialName: this.materialName,
          page: this.page - 1,
          size: this.pageSize
        }
        this.SafemanageListService.getMaterialList(_dataJson).subscribe(res => {
          this.bottomListOfData = res.content
          this.totalSize = res.totalElements
          if(this.totalSizeTop==0){
            this.bottomListOfData = []
          }
        })
        resolve()
      } catch (e) {
        reject(e)
      }
    })
  }
  clickPage(index) {
    this.page = index
    this.getMaterialList()
  }
  isAllDisplayDataChecked = false;
  isIndeterminate = false;
  listOfDisplayData = [];
  bottomListOfData = []
  mapOfCheckedId: { [key: string]: boolean } = {};
  numberOfChecked = 0;
  idList = [];//批量修改
  currentPageDataChange($event): void {
    this.listOfDisplayData = $event;
    this.refreshStatus();
  }
  refreshStatus(): void {
    this.isAllDisplayDataChecked = this.listOfDisplayData
      .every(item => this.mapOfCheckedId[item.id]);
    if (this.listOfDisplayData.length == 0) {
      this.isAllDisplayDataChecked = false
    }
    this.isIndeterminate =
      this.listOfDisplayData.some(item => this.mapOfCheckedId[item.id]) &&
      !this.isAllDisplayDataChecked;
    this.numberOfChecked = this.bottomListOfData.filter(item => this.mapOfCheckedId[item.id]).length;
    let checkedList = this.bottomListOfData.filter(item => this.mapOfCheckedId[item.id])
    let ids = []
    checkedList.forEach(item => {
      ids.push(item.id)
    })
    this.idList = ids
  }
  checkAll(value: boolean): void {
    this.listOfDisplayData.filter(item => !item.disabled).forEach(item => (this.mapOfCheckedId[item.id] = value));
    this.refreshStatus();
  }

  editKC() {
    if (this.selectedKFId && this.selectedKQId) {
      this.NzModalService.confirm({
        nzTitle: '确认保存吗？',
        nzOkText: '确认',
        nzCancelText: '取消',
        nzOnOk: () => {
          let checkedList = this.bottomListOfData.filter(item => this.mapOfCheckedId[item.id])
          let _newData = []
          checkedList.forEach(item=>{
            _newData.push({
              id:item.id,
              inStockCycle: Number(item.inStockCycle),
              maxAmount: item.maxAmount,
              purchasingCycle: item.purchasingCycle,
              safeAmount: item.safeAmount
            })
          })
          // let _data = {
          //   id: checkedList[0].id,
          //   inStockCycle: Number(checkedList[0].inStockCycle),
          //   maxAmount: checkedList[0].maxAmount,
          //   purchasingCycle: checkedList[0].purchasingCycle,
          //   safeAmount: checkedList[0].safeAmount
          // }
          this.SafemanageListService.editKC(_newData).subscribe(res => {
            this.getMaterialList()
            this.mapOfCheckedId = {}
          })
        }
      })
    } else {
      this.NzMessageService.create("error", "请选择库房库区!")
      this.mapOfCheckedId = {}
      this.idList = []
      this.numberOfChecked = 0
      this.isIndeterminate = false
      this.getMaterialList()
    }
  }
  // table 2 end

}
